/**
*
 * Genarator by KKDGen v<?php echo $version."\n";?>
 * @author KKDeveloeper
 * @email Dixonsatit@gmail.com
 * @link www.ikkdev.com,www.kkdeveloper.com
 <?php  echo empty($nameSpace)?'':'* Namespace : '.$nameSpace."\n"; ?>
 * ExtjsGrid : <?php echo $modelClass."\n";?>
 */

Ext.define('<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.view.'.strtolower($modelClass).'.grid' ?>', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.<?php echo strtolower($modelClass); ?>Grid', 
    border: false,
    initComponent: function() {
        Ext.apply(this, {
            selModel:Ext.create('Ext.selection.CheckboxModel'),
            store: '<?php echo $modelClass?>',
            columns: [Ext.create('Ext.grid.RowNumberer'),<?php 
$count = count($columns);
$i=1;
foreach ($columns as $column): 
    
    if($column->type=='integer')$type='int';
    elseif($column->type=='date')$type='date';
    else $type='string';
    
    $dot = $count==$i?"\n":",\n";
    $flex = $i==1?'flex:1':'width:150';
    echo "{
                text: '".ucfirst($column->name)."',
                dataIndex: '$column->name',
                $flex,
                 field: {
                        xtype: 'textfield',
                        allowBlank:false
                    }
            }$dot";
    $i++;
    
endforeach; 
?>],
            dockedItems:[{
                xtype: 'toolbar',
                dock: 'top',
                items: [{
                    text: 'Add',
                    iconCls: 'icon-add',
                    action: 'add', 
                    tooltip: 'เพื่มข้อมูล',
                    scope: this
                },{
                    text: 'Delete',
                    iconCls: 'icon-delete',
                    tooltip: 'ลบข้อมูลแถวที่เลือก?',
                    action: 'delete',
                    itemId: 'deleteIngird',
                    disabled: true,
                    scope:this,
                    handler: this.onDeleteClick
                }, '->','Search',
               {
                xtype: 'searchfield',
                store: Ext.getStore('<?php echo $modelClass?>') 
              }]
            }],
            plugins: [Ext.create('Ext.grid.plugin.RowEditing', {
                clicksToEdit: 0,
                errorSummary: true,
                listeners:{
                    edit:function(){
                    
                    }
                }
            })],
            bbar: Ext.create('Ext.PagingToolbar', {
                store: '<?php echo $modelClass?>',
                displayInfo: true,
                displayMsg: 'Displaying topics {0} - {1} of {2}',
                emptyMsg: "No topics to display"
            })
        });

        this.callParent(arguments);
        this.getSelectionModel().on('selectionchange', this.onSelectChange, this);
    // this.on('itemdblclick',function(){alert('sdf')},this);
    },
    onSelectChange: function(selModel, selections){
        this.down('#deleteIngird').setDisabled(selections.length === 0);
    },
    onDeleteClick: function(){
        
        Ext.Msg.show({
            title:'Confirm Delete',
            msg: 'คุณต้องการลบข้อมุลใช่หรือไม่..',
            buttons: Ext.Msg.YESNO,
            fn: this.onDelete,
            icon: Ext.MessageBox.WARNING,
            scope: this
        });
        
    },
    onDelete: function(btn)
    {
        if(btn=='yes')
        {
            var selection = this.getView().getSelectionModel().getSelection();
            for (var i=0;i<selection.length;i++) 
            {
                console.log('===== selections ===== : '+selection[i].get("id"));
            }
            // selection = this.getView().getSelectionModel().getSelection()[0];
            if (selection) 
            {
                this.store.remove(selection);
            }
            return true;
        }
        else return false;
 
    }
});


